package com.jdy.haoduoai.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jdy.haoduoai.Constant;
import com.jdy.haoduoai.LeXiaoXiaoBanApp;
import com.jdy.haoduoai.intface.IAsyncTask;
import com.jdy.haoduoai.model.LeMachine;
import com.jdy.haoduoai.model.LeUser;
import com.jdy.haoduoai.parse.JsonSerializer;
import com.jdy.haoduoai.util.CustomAsyncTask;
import com.jdy.haoduoai.util.HttpUtils;
import com.jdy.haoduoai.util.ImageUtils;
import com.jdy.haoduoai.util.LeConfig;
import com.jdy.haoduoai.util.PathUtil;
import com.jdy.haoduoai.util.ResponseResult;
import com.jdy.haoduoai.util.StringInfoMatch;
import com.jdy.haoduoai.util.Tools;
import com.jdy.haoduoai.view.YearMonthDayPicker;
import com.ycsj.goldmedalnewconcept.R;

import org.droidparts.annotation.inject.InjectView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

//import com.jdy.haoduoai.R;

/**
 * 
 * 
 * @author hf
 * 
 */
@SuppressLint("InflateParams")
public class FillInfoPopupActivity extends BaseActivity implements
    OnClickListener, OnCheckedChangeListener, TextWatcher {

  private final static int REQUEST_PARENT_IMAGE = 100;
  private final static int REQUEST_BABY_IMAGE = 101;

  private static final int REQUIRED_WIDTH = 300;
  private static final int REQUIRED_HEIGHT = 300;

  private RelativeLayout girllyt;
  private RelativeLayout boylyt;
  private RadioButton radioButton1;
  private RadioButton radioButton2;
  private Button enter;

  @InjectView(id = R.id.babyImageView, click = true)
  private ImageView babyImageView;
  @InjectView(id = R.id.parentImageView, click = true)
  private ImageView parentImageView;
  @InjectView(id = R.id.edittext_babyname, click = true)
  private EditText edittext_babyname;
  @InjectView(id = R.id.textview_birth, click = true)
  private TextView textview_birth;
  @InjectView(id = R.id.parent_nickname_edit, click = true)
  private EditText parent_nickname_edit;
  @InjectView(id = R.id.appellations_textview, click = true)
  private TextView appellations_textview;
  @InjectView(id = R.id.tv_ch, click = true)
  private TextView tv_ch;

  @InjectView(id = R.id.edittext_tall, click = true)
  private EditText edittext_tall;
  @InjectView(id = R.id.edittext_weight, click = true)
  private EditText edittext_weight;

  private int mGender = 1;
  private List<String> list = new ArrayList<String>();
  private AlertDialog.Builder builder;
  private AlertDialog alertDialog;
  private String parentAvatar, babyAvatar;
  private String mApellationName = "爸爸";
  private LeUser mLeUser;
  private LeMachine mLeBaby;
  private LeUser mLeUserGetted;
  private Uri BabyUri;
  private Uri ParentUri;
  private String[] mArrName;

  private String fromPage;

  // 是否首次注册
  // private boolean isFirstRegister;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.popuwindow);
    setTitle("填写信息");

    initData();
    initView();
    initListener();

    getBabyInfo();
  }

  private void initData() {
    mArrName = getResources().getStringArray(R.array.appellation_array_name);
    mLeUser = LeXiaoXiaoBanApp.getInstance().getUser();
    // isFirstRegister =
    // LePreference.getInstance().getBoolean("isFirstRegister",
    // true);

    Intent ttt = getIntent();
    if (ttt != null) {
      fromPage = ttt.getStringExtra("fromPage");
    }

    list = Arrays.asList(mArrName);

    if (mArrName != null && mArrName.length > 0
        && Tools.isNotEmpty(mLeUser.callname)) {
      for (int i = 0; i < mArrName.length; i++) {
        if (mArrName[i].contains(mLeUser.callname)) {
          tv_ch.setText(mArrName[i]);
          break;
        }
      }
    }
  }

  private Bitmap setImageViewContent(ImageView source, Uri original) {
    if (original != Uri.EMPTY) {
      Bitmap originalBitmap = ImageUtils.getBitmapFromUri(this, original);
      if (originalBitmap != null) {
        Bitmap bm = ImageUtils.scaleCropToFit(originalBitmap, REQUIRED_WIDTH,
            REQUIRED_HEIGHT, true);
        source.setImageURI(null);
        if (bm != null)
          source.setImageBitmap(bm);
        return bm;
      }
    }
    return null;
  }

  @Override
  public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
      if (Tools.isNotEmpty(fromPage)) {
        if (fromPage.contains("BindDeviceActivity")) {
          Intent t = new Intent(this, ConnectWifiActivity.class);
          t.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
          startActivity(t);
        } else if (fromPage.contains("LoginActivity")) {
          Intent intent0 = new Intent(this, LoginActivity.class);
          intent0.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
          startActivity(intent0);
        }

        finish();
        return true;
      } else {
        return super.onKeyDown(keyCode, event);
      }

    }
    return super.onKeyDown(keyCode, event);
  }

  private void uploadBabyAvatar(final String filePath) {
    new CustomAsyncTask(this, new IAsyncTask() {
      @Override
      public void onRecieveData(Activity activity, ResponseResult result) {
        if (result != null) {
          babyAvatar = result.data;
        }
      }

      @Override
      public ResponseResult doInbackground(Activity activity) {
        HashMap<String, Object> param_ = new HashMap<String, Object>();
        param_.put(HttpUtils.TAG_MOBILE_I, mLeUser.id);
        param_.put(HttpUtils.TAG_KEY_I, HttpUtils.KEY);
        param_.put(HttpUtils.TAG_MODULE_I, "member/machine/avatar");

        return HttpUtils.startUploadRequest(HttpUtils.API_URL
            + HttpUtils.UPLOAD_BABY_AVATAR_POST, param_, "Filedata", filePath,
            HttpUtils.TAG_POST_REQUEST);

      }
    }).execute("请检查网络连接!");
  }

  private void uploadParentAvatar(final String filePath) {
    new CustomAsyncTask(this, new IAsyncTask() {
      @Override
      public void onRecieveData(Activity activity, ResponseResult result) {
        if (result != null) {
          parentAvatar = result.data;
        }
      }

      @Override
      public ResponseResult doInbackground(Activity activity) {
        HashMap<String, Object> param_ = new HashMap<String, Object>();
        param_.put(HttpUtils.TAG_MOBILE_I, mLeUser.id);
        param_.put(HttpUtils.TAG_KEY_I, HttpUtils.KEY);
        param_.put(HttpUtils.TAG_MODULE_I, "member/avatar");

        return HttpUtils.startUploadRequest(HttpUtils.API_URL
            + HttpUtils.UPLOAD_AVATAR_METHOD_POST, param_, "Filedata",
            filePath, HttpUtils.TAG_POST_REQUEST);
      }
    }).execute("请检查网络连接!");
  }

  @Override
  protected void onActivityResult(int requestCode, int resultCode,
      Intent returnedIntent) {
    super.onActivityResult(requestCode, resultCode, returnedIntent);
    switch (requestCode) {
    case REQUEST_BABY_IMAGE:
      if (resultCode == RESULT_OK) {
        this.BabyUri = Uri.parse(returnedIntent.getExtras().getString("image"));
        setImageViewContent(babyImageView, BabyUri);
        uploadBabyAvatar(this.BabyUri.getPath());
        storeUserInfo(Constant.BABY_AVATAR, BabyUri.toString());

        updateBtnStatus();
      }
      break;
    case REQUEST_PARENT_IMAGE:
      if (resultCode == RESULT_OK) {
        this.ParentUri = Uri.parse(returnedIntent.getExtras()
            .getString("image"));
        setImageViewContent(parentImageView, this.ParentUri);
        uploadParentAvatar(this.ParentUri.getPath());
        storeUserInfo(Constant.PARENT_AVATAR, this.ParentUri.toString());

        updateBtnStatus();
      }
      break;

    }
  }

  private void storeUserInfo(String key, String value) {
    SharedPreferences prefs = this.getSharedPreferences(Constant.USER_INFO,
        Context.MODE_PRIVATE);
    prefs.edit().putString(key, value).apply();
  }

  private void initView() {

    girllyt = (RelativeLayout) findViewById(R.id.girllyt);
    boylyt = (RelativeLayout) findViewById(R.id.boylyt);
    radioButton1 = (RadioButton) findViewById(R.id.radio1);
    radioButton2 = (RadioButton) findViewById(R.id.radio2);
    enter = (Button) findViewById(R.id.click);
    enter.setEnabled(false);

    mBackImageButton.setOnClickListener(new OnClickListener() {

      @Override
      public void onClick(View v) {
        if (Tools.isNotEmpty(fromPage)) {
          if (fromPage.contains("BindDeviceActivity")) {
            Intent t = new Intent(FillInfoPopupActivity.this,
                ConnectWifiActivity.class);
            t.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
            startActivity(t);
          } else if (fromPage.contains("LoginActivity")) {
            Intent intent0 = new Intent(FillInfoPopupActivity.this,
                LoginActivity.class);
            intent0.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
            startActivity(intent0);
          }

        }

        finish();
      }
    });

    babyImageView.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {

        Intent intent = new Intent(FillInfoPopupActivity.this,
            SelectPicPopupWindow.class);
        intent.putExtra("avatar_path",
            PathUtil.getBabyAvatarPath(getApplicationContext()));
        startActivityForResult(intent, REQUEST_BABY_IMAGE);
        overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
      }
    });

    parentImageView.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        Intent intent = new Intent(new Intent(FillInfoPopupActivity.this,
            SelectPicPopupWindow.class));
        intent.putExtra("avatar_path",
            PathUtil.getElderAvatarPath(getApplicationContext()));
        startActivityForResult(intent, REQUEST_PARENT_IMAGE);
        overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
      }
    });
  }

  private void initListener() {
    girllyt.setOnClickListener(this);
    boylyt.setOnClickListener(this);
    enter.setOnClickListener(this);
    enter.setClickable(false);
    enter.setEnabled(false);
    edittext_babyname.addTextChangedListener(this);
    parent_nickname_edit.addTextChangedListener(this);

    edittext_tall.addTextChangedListener(this);
    edittext_weight.addTextChangedListener(this);

  }

  public void ShowDialog() {
    Context context = this;
    LayoutInflater inflater = (LayoutInflater) context
        .getSystemService(LAYOUT_INFLATER_SERVICE);
    View layout = inflater.inflate(R.layout.formcommonlist, null);
    ListView myListView = (ListView) layout
        .findViewById(R.id.formcustomspinner_list);
    MyAdapter adapter = new MyAdapter(context, list);
    myListView.setAdapter(adapter);
    myListView.setOnItemClickListener(new OnItemClickListener() {

      @Override
      public void onItemClick(AdapterView<?> arg0, View arg1, int positon,
          long id) {
        mApellationName = list.get(positon).toString();
        tv_ch.setText(mApellationName);

        updateBtnStatus();

        if (alertDialog != null) {
          alertDialog.dismiss();
        }
      }
    });
    builder = new AlertDialog.Builder(context);
    builder.setView(layout);
    alertDialog = builder.create();
    alertDialog.show();
  }

  class MyAdapter extends BaseAdapter {
    private List<String> mlist;
    private Context mContext;

    public MyAdapter(Context context, List<String> list) {
      this.mContext = context;
      mlist = new ArrayList<String>();
      this.mlist = list;
    }

    @Override
    public int getCount() {
      return mlist.size();
    }

    @Override
    public Object getItem(int position) {

      return mlist.get(position);
    }

    @Override
    public long getItemId(int position) {
      return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
      Person person = null;
      if (convertView == null) {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        convertView = inflater.inflate(R.layout.rtu_item, null);
        person = new Person();
        person.name = (TextView) convertView.findViewById(R.id.tv_name);
        convertView.setTag(person);
      } else {
        person = (Person) convertView.getTag();
      }
      person.name.setText(list.get(position).toString());
      return convertView;
    }

    class Person {
      TextView name;
    }
  }

  @Override
  public void onClick(View v) {
    Intent intent = new Intent();
    switch (v.getId()) {

    case R.id.tv_ch:
      ShowDialog();
      break;

    case R.id.girllyt:
      if (!radioButton1.isChecked()) {
        radioButton1.setChecked(true);
        radioButton2.setChecked(false);
        if (Tools.isNullStr(BabyUri)) {
          babyImageView.setImageResource(R.drawable.defaut_baby_avtar_female);
        }
      }
      break;
    case R.id.boylyt:
      if (!radioButton2.isChecked()) {
        radioButton2.setChecked(true);
        radioButton1.setChecked(false);

        if (Tools.isNullStr(BabyUri)) {
          babyImageView.setImageResource(R.drawable.defaut_baby_avtar_male);
        }
      }
      break;
    case R.id.textview_birth:
      final YearMonthDayPicker picker = new YearMonthDayPicker(this);
      picker.build(new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
          textview_birth.setText(picker.getSelectedDate());

          updateBtnStatus();
        }
      }, null);
      picker.show();
      break;
    case R.id.click:
      if (Tools.isEmpty(edittext_babyname.getText().toString())
          || Tools.isEmpty(textview_birth.getText().toString())
          || Tools.isEmpty(parent_nickname_edit.getText().toString())
          || Tools.isEmpty(tv_ch.getText().toString())) {
        break;
      }

      String height = edittext_tall.getText().toString().trim();
      String weight = edittext_weight.getText().toString().trim();
      if (Tools.isNotEmpty(height) && !stringMatch.ifMatchHeightFormat(height)) {
        toastShow("请输入正确身高!");
        break;
      }

      if (Tools.isNotEmpty(weight) && !stringMatch.ifMatchWeightFormat(weight)) {
        toastShow("请输入正确体重!");
        break;
      }

      LeConfig.isInfoFilled = true;

      if (radioButton1.isChecked()) {
        mGender = 0;
      } else if (radioButton2.isChecked()) {
        mGender = 1;
      }

      updateBabyInfo();
      break;
    case R.id.close:
      intent.setAction(LeConfig.QuitPopuWindow);
      sendBroadcast(intent);
      this.finish();
      break;
    default:
      break;
    }
  }

  private final StringInfoMatch stringMatch = new StringInfoMatch();

  private void updateBabyInfo() {
    if (Tools.isNotEmpty(edittext_babyname.getText().toString().trim())
        && Tools.isNotEmpty(textview_birth.getText().toString().trim())
        || (radioButton1.isChecked() || radioButton2.isChecked())) {

      new CustomAsyncTask(this, new IAsyncTask() {
        @Override
        public void onRecieveData(Activity activity, ResponseResult result) {
          if (result.isSuccess()) {
            toastShow(result.data);

            updateMachineInfo();

            updateParentInfo();
          }
        }

        @Override
        public ResponseResult doInbackground(Activity activity) {

          HashMap<String, Object> param_ = new HashMap<String, Object>();
          param_.put(HttpUtils.TAG_SNO_I, mLeUser.sno);
          param_.put(HttpUtils.TAG_KEY_I, HttpUtils.KEY);
          param_.put("machine_id", mLeUser.machine.id);
          param_.put("name", edittext_babyname.getText().toString().trim());
          param_.put("avatar", babyAvatar);
          param_.put("gender", mGender);
          param_.put("dob", textview_birth.getText().toString().trim());

          String height = edittext_tall.getText().toString().trim();
          String weight = edittext_weight.getText().toString().trim();

          if (Tools.isNotEmpty(height)) {
            param_.put("height", height);
          }

          if (Tools.isNotEmpty(weight)) {
            param_.put("weight", weight);
          }

          return HttpUtils.startRequest(HttpUtils.API_URL
              + "/api/2.0/member/machine/" + mLeUser.machine.id, param_,
              HttpUtils.TAG_PUT_REQUEST);
        }
      }).execute("请检查网络连接!");

    }
  }

  private void updateUserInfo() {
    LeUser lu = LeXiaoXiaoBanApp.getInstance().getUser();

    if (lu != null) {
      lu.token = "test";
      lu.platform = "android";
      lu.avatar = parentAvatar;
      lu.nickname = parent_nickname_edit.getText().toString().trim();
      lu.callname = mApellationName;
    }

    LeXiaoXiaoBanApp.getInstance().setUser(lu);
    Tools.saveLeUser(lu);
  }

  private void updateMachineInfo() {
    LeMachine baby = LeXiaoXiaoBanApp.getInstance().getBaby();
    if (baby != null) {
      baby.id = mLeUser.machine.id;
      baby.name = edittext_babyname.getText().toString().trim();
      baby.avatar = babyAvatar;
      baby.gender = mGender;
      baby.dob = textview_birth.getText().toString().trim();
    }

    LeXiaoXiaoBanApp.getInstance().setBaby(baby);
    Tools.saveLeBaby(baby);

    LeUser lu = LeXiaoXiaoBanApp.getInstance().getUser();
    if (lu != null) {
      lu.machine = baby; // );
    }
  }

  private int totalMonth;

  /**
   * 判断宝宝年龄
   */
  private void checkBabyAge() {
    LeUser lu = LeXiaoXiaoBanApp.getInstance().getUser();
    if (lu != null && Tools.isNotEmpty(lu.machine)) {
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

      try {
        Calendar bef = Calendar.getInstance();
        bef.setTime(sdf.parse(lu.machine.dob));

        int month = Calendar.getInstance().get(Calendar.MONTH)
            - bef.get(Calendar.MONTH);

        int year = Calendar.getInstance().get(Calendar.YEAR)
            - bef.get(Calendar.YEAR);

        totalMonth = year * 12 + month;
        if (totalMonth == 0) {
          totalMonth = 1;
        }

      } catch (Exception e) {
        // TODO: handle exception
      }
    }
  }

  @Override
  protected void onDestroy() {
    // LePreference.getInstance().save("isFirstRegister", false);

    super.onDestroy();
  }

  private void updateParentInfo() {
    mApellationName = tv_ch.getText().toString();

    if (Tools.isNotEmpty(parent_nickname_edit.getText().toString().trim())
        && Tools.isNotEmpty(mApellationName)) {

      new CustomAsyncTask(this, new IAsyncTask() {
        @Override
        public void onRecieveData(Activity activity, ResponseResult result) {
          if (result.isSuccess()) {
            toastShow(result.data);

            updateUserInfo();

            checkBabyAge();

            Intent intent = new Intent(FillInfoPopupActivity.this,
                    MyPainPointSelectActivity.class);
            intent.putExtra("fromFillInfoPage", true);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);

            // if (isFirstRegister) {
            // Intent intent = new Intent(FillInfoPopupActivity.this,
            // PainPointSelectActivity.class);
            // intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            // startActivity(intent);
            // } else {
            // if (totalMonth >= (6 * 12)) {
            // Intent t = new Intent(FillInfoPopupActivity.this,
            // AttractNewRepelActivity.class);
            // startActivity(t);
            // } else {
            // Intent intent = new Intent();
            // intent.putExtra("registerflow", true);
            // intent.setClass(FillInfoPopupActivity.this,
            // AbilityTestActivity.class);
            // startActivity(intent);
            // }
            // }

            finish();
          }
        }

        @Override
        public ResponseResult doInbackground(Activity activity) {

          HashMap<String, Object> param_ = new HashMap<String, Object>();
          param_.put(HttpUtils.TAG_SNO_I, mLeUser.sno);
          param_.put(HttpUtils.TAG_KEY_I, HttpUtils.KEY);
          param_.put("token", "test");
          param_.put("platform", "android");
          param_.put("avatar", parentAvatar);
          param_.put("nickname", parent_nickname_edit.getText().toString()
              .trim());
          param_.put("callname", mApellationName);

          return HttpUtils.startRequest(HttpUtils.API_URL + "/api/2.0/member/"
              + mLeUser.sno, param_, HttpUtils.TAG_PUT_REQUEST);
        }
      }).execute("请检查网络连接!");

    }
  }

  @Override
  public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    switch (buttonView.getId()) {
    case R.id.radio1:
      if (isChecked) {
        radioButton2.setChecked(false);

        updateBtnStatus();

        if (Tools.isNullStr(BabyUri)) {
          babyImageView.setImageResource(R.drawable.defaut_baby_avtar_male);
        }
      }
      break;
    case R.id.radio2:
      if (isChecked) {
        radioButton1.setChecked(false);

        updateBtnStatus();

        if (Tools.isNullStr(BabyUri)) {
          babyImageView.setImageResource(R.drawable.defaut_baby_avtar_male);
        }
      }
      break;
    default:
      break;
    }
  }

  @Override
  public void beforeTextChanged(CharSequence s, int start, int count, int after) {
  }

  @Override
  public void onTextChanged(CharSequence s, int start, int before, int count) {
    updateBtnStatus();
  }

  @Override
  public void afterTextChanged(Editable s) {
    // TODO
  }

  private void getParentInfo() {
    if (mLeUser != null) {
      new CustomAsyncTask(this, new IAsyncTask() {
        @Override
        public void onRecieveData(Activity activity, ResponseResult result) {
          if (result.isSuccess() && Tools.isNotNullStr(result.data)) {
            mLeUserGetted = JsonSerializer.getInstance().deserialize(
                result.data, LeUser.class);

            if (Tools.isNotEmpty(mLeUserGetted)) {
              parent_nickname_edit.setText(mLeUserGetted.nickname);
              mApellationName = mLeUserGetted.callname;
            }
          }

          updateBtnStatus();
          dismissLoading();
        }

        @Override
        public ResponseResult doInbackground(Activity activity) {
          HashMap<String, Object> param_ = new HashMap<String, Object>();
          param_.put(HttpUtils.TAG_KEY_I, HttpUtils.KEY);

          return HttpUtils.startRequest(HttpUtils.API_URL + "/api/2.0/member/"
              + mLeUser.sno, param_, HttpUtils.TAG_GET_REQUEST);
        }
      }).execute("请检查网络连接!");
    }

  }

  private void getBabyInfo() {

    showloading("");

    if (mLeUser != null && mLeUser.machine != null
        && Tools.isNotEmpty(mLeUser.machine.id)) {
      new CustomAsyncTask(this, new IAsyncTask() {
        @Override
        public void onRecieveData(Activity activity, ResponseResult result) {
          if (result.isSuccess() && Tools.isNotNullStr(result.data)) {
            mLeBaby = JsonSerializer.getInstance().deserialize(result.data,
                LeMachine.class);

            if (mLeBaby != null) {
              edittext_babyname.setText(mLeBaby.name);
              textview_birth.setText(mLeBaby.dob);

              if (mLeBaby.gender == 0) { // female
                radioButton1.setChecked(true);
                radioButton2.setChecked(false);
                if (Tools.isNullStr(BabyUri)) {
                  babyImageView
                      .setImageResource(R.drawable.defaut_baby_avtar_female);
                }
              } else { // male
                radioButton2.setChecked(true);
                radioButton1.setChecked(false);
                if (Tools.isNullStr(BabyUri)) {
                  babyImageView
                      .setImageResource(R.drawable.defaut_baby_avtar_male);
                }
              }
            }
          }

          getParentInfo();
        }

        @Override
        public ResponseResult doInbackground(Activity activity) {

          HashMap<String, Object> param_ = new HashMap<String, Object>();
          param_.put(HttpUtils.TAG_SNO_I, mLeUser.sno);
          param_.put(HttpUtils.TAG_KEY_I, HttpUtils.KEY);

          return HttpUtils.startRequest(HttpUtils.API_URL
              + "/api/2.0/member/machine/" + mLeUser.machine.id, param_,
              HttpUtils.TAG_GET_REQUEST);
        }
      }).execute("请检查网络连接!");
    }

  }

  private void updateBtnStatus() {
    if (Tools.isNotEmpty(textview_birth.getText().toString().trim())
        && Tools.isNotEmpty(edittext_babyname.getText().toString().trim())
        && (radioButton1.isChecked() || radioButton2.isChecked())
        && Tools.isNotEmpty(parent_nickname_edit.getText().toString().trim())
        && Tools.isNotEmpty(tv_ch.getText().toString().trim())) {
      enter.setClickable(true);
      enter.setEnabled(true);
      enter.setBackgroundResource(R.drawable.round_btn_selector);
    } else {
      enter.setClickable(false);
      enter.setEnabled(false);
      enter.setBackgroundResource(R.drawable.unable_bg);
    }
  }

}
